﻿package core.dialog {
	import flash.display.MovieClip;
	import flash.text.TextField;
	import core.events.CmdEvent;
	import core.net.socket.cmd.LoginCmd;
	import core.events.LoginEvent;
	import base.BaseMc;
	import flash.events.MouseEvent;
	import flash.events.FocusEvent;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import core.net.socket.cmd.LoginCmd;
	import core.net.socket.cmd.GetUserInfoCmd;
	import core.net.socket.cmd.SoftKeyBoardOpenCmd;
	import core.net.socket.cmd.SoftKeyBoardCloseCmd;
	import caurina.transitions.Tweener;
	import core.net.socket.cmd.NetWorkConnectCmd;
	import core.dialog.DialogCheckNet;
	import core.net.socket.cmd.WindowQuiteCmd;
	import core.User;
	import core.net.socket.cmd.GetBarCodeCmd;
	
	public class DialogLogin extends BaseMc {
	
		//手机号和密码输入框
		private var tfMobile:TextField;
		private var tfPassword:TextField;
		private var btnLogin:LoginBtn;
		private var checkBox:CheckBox;
		private var back:Boolean = false;
		private var dlgCheckNet:DialogCheckNet;
		
		public function DialogLogin(back:Boolean = false){
			this.back = back;
		}
		
		protected override function onInit():void {
			trace("DialogLogin onInit");
			this.initMobileField();
			this.initPasswordField();
			this.initLoginBtn();
			//this.initCheckBox();
			this.centerOfStage();
			this.y = 0;
			this.dlgCheckNet = new DialogCheckNet();
			this.dlgCheckNet.btnExit.addEventListener(MouseEvent.CLICK,onCheckNetExit);
			this.sendCmd(GetUserInfoCmd.make());
		}
		
		
		private function onCheckNetExit(e:MouseEvent):void {
			this.sendCmd(WindowQuiteCmd.make());
		}
		
		protected override function onRemove():void {
			super.onRemove();
		}
		
		
		
		
		//复选框初始化
		private function initCheckBox():void {
			this.checkBox=new CheckBox();
			this.checkBox.x=450;
			this.checkBox.y=440;
			this.addChild(checkBox);
			
		}
		//账号框初始化
		private function initMobileField():void {
			this.tfMobile = new TextField();
			this.tfMobile.x=250;
			this.tfMobile.y=225;
			this.tfMobile.width=340;
			this.tfMobile.height=50; 
			this.tfMobile.borderColor=0x000000;
			this.tfMobile.type=TextFieldType.INPUT; 
			var tfMobilef:TextFormat= new TextFormat();
			tfMobilef.size=40;
			tfMobilef.color=0xffffff;  
			tfMobilef.bold=true;
			this.tfMobile.defaultTextFormat=tfMobilef;
			this.addChild(this.tfMobile);
			this.tfMobile.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
            //this.tfMobile.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);
            
		}
		
		//密码框初始化
		private function initPasswordField():void {
			this.tfPassword = new TextField();
			this.tfPassword.x=250;
			this.tfPassword.y=330;
			this.tfPassword.width=340;
			this.tfPassword.height=50; 
			this.tfPassword.borderColor=0x000000;
			this.tfPassword.type=TextFieldType.INPUT;
			this.tfPassword.borderColor=0x000000;
			this.tfPassword.type=TextFieldType.INPUT;
			this.tfPassword.displayAsPassword = true;
			var tfMobilef:TextFormat= new TextFormat();
			tfMobilef.size=50;
			tfMobilef.color=0xffffff;  
			tfMobilef.bold=true;
			this.tfPassword.defaultTextFormat=tfMobilef;
			this.addChild(this.tfPassword);
			this.tfPassword.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
            //this.tfPassword.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);
			
		}
		//登录初始化
		private function initLoginBtn():void {
			this.btnLogin = new LoginBtn();
			this.btnLogin.scaleX *= 0.7;
			this.btnLogin.scaleY *= 0.7;
			this.btnLogin.x=385;
			this.btnLogin.y=480;
			this.addChild(this.btnLogin);
			this.btnLogin.addEventListener(MouseEvent.CLICK,loginClick);
		}
		//登录点击事件
		private function loginClick(e:MouseEvent):void
		{
			focusOutHandler(null);//关闭软键盘
			var mobile=this.tfMobile.text;
			var password=this.tfPassword.text;
			
			if(mobile == "" || password == ""){
				var dlg:DialogMessage = new DialogMessage("用户名或密码不能为空",true);
				this.stage.addChild(dlg);
				return;
			}

			this.sendCmd(LoginCmd.make(mobile,password,true));
			this.dispatchEvent(new LoginEvent(LoginEvent.LOGIN,null));
		}
		
		public function login():void {
			this.loginClick(null);
		}
		
		 private function focusInHandler(event:FocusEvent):void {
          
            this.sendCmd(SoftKeyBoardOpenCmd.make());
            trace("focusInHandler: focusInHandler"  );
        }

        private function focusOutHandler(event:FocusEvent):void {
           this.sendCmd(SoftKeyBoardCloseCmd.make()); 
		   trace("focusOutHandler: focusOutHandler" );
        }
		
		protected override function onCommandResponse(e:CmdEvent):void {
			super.onCommandResponse(e);
			var data:Object = e.data;
			if(data.cmd == LoginCmd.COMMAND_ID){
				if(data.error == ""){
					User.id = data.data.user_id;
					
					//获取BarCode
					this.sendCmd(GetBarCodeCmd.make());
					
				}else{
					//登录失败
					//data.error
					dispatchEvent(new LoginEvent(LoginEvent.FAILED,data));
				}
			}else if(data.cmd == GetUserInfoCmd.COMMAND_ID){
				if(data.error != ""){
					return;
				}
				if(data.data.mobile){
					this.tfMobile.text = data.data.mobile;
				}
				
				if(data.data.password){
					this.tfPassword.text = data.data.password;
				}
				
				User.id = data.data.user_id;
				User.deviceAddress = data.data.address;
				
				if(data.data.auto == "True"){
					//this.checkBox.check();
					if(!this.back)
					{
						//this.loginClick(null);
						//this.sendCmd(NetWorkConnectCmd.make());
						dispatchEvent(new LoginEvent(LoginEvent.SUCCESS,{}));
					}
				}
				//不检测网络，直接启动到主场景
				
				
			}else if(data.cmd == NetWorkConnectCmd.COMMAND_ID){
				if(data.error == "true"){
					this.dlgCheckNet.remove();
					this.loginClick(null);
					trace("网络通畅");
			     }else{
					trace("网络不通");
					if(this.dlgCheckNet.parent == null){
						this.stage.addChild(this.dlgCheckNet);
					}
					this.sendCmd(NetWorkConnectCmd.make());
				 }
			}else if(data.cmd == GetBarCodeCmd.COMMAND_ID){
				if(data.error == ""){
					User.deviceAddress = data.data.address;
				}else{
					User.deviceAddress = "";
				}
				dispatchEvent(new LoginEvent(LoginEvent.SUCCESS,{}));
			}
		}
	}
	
}
